﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AMS.DataAccess.BO;
using AMS.DataAccess.Interfaces;
using System.Text;
using AMS.DataAccess.BO.Enums;

public partial class Customer_AddCustomerSimpleDlg : BaseWorkPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        btnCancel.Attributes["onclick"] = "javascript:onCancel();return false;";
    }

    protected void btnOK_Click(object sender, EventArgs e)
    {
        CustomerInfo customer = new CustomerInfo();
        ContactorInfo contactor = new ContactorInfo();
        if (!ucCusInfo.GetCustomer(customer))
        {
            return;
        }
        else
        {
            customer.IsCustomer = true;
            customer.BusinessType = CustomerBusinessType.Client;
        }

        if (!ucContInfo.RetriveContactor(contactor))
        {
            return;
        }
        else
        {
            contactor.isFirst = true;
        }

        // add to database
        // TODO: add transaction control herere
        ICustomerManager customerMgr = ManagerFactory.CreateCustomerManager();
        IContactorManager contactorMgr = ManagerFactory.CreateContactorManager();
        customer.AID = customerMgr.AddCustomer(CallContext, customer);
        contactor.CustomerID = customer.AID;
        List<ContactorInfo> contactors =new List<ContactorInfo>();
        contactors.Add(contactor);
        contactorMgr.AddContactors(CallContext, contactors);

        StringBuilder sbJson = new StringBuilder();
        sbJson.Append("{");
        sbJson.AppendFormat("id:'{0}',", customer.AID);
        sbJson.AppendFormat("name:'{0}'", customer.Name);
        sbJson.Append("}");

        StringBuilder sbscript = new StringBuilder();
        sbscript.Append("setTimeout(function(){window.parent.document.onDialogCallBack(1, 'succeed',");
        sbscript.Append(sbJson);
        sbscript.Append(");}, 3000);");

        lblMsg.Text = "添加成功, 3秒钟将关闭对话框。";
        btnOK.Enabled = false;
        ClientScript.RegisterStartupScript(GetType(),
            "BCF145CA-3290-43A0-9543-D4EB12506FB9",
            sbscript.ToString()
            ,true);
    }
}
